﻿@model RedisMemoryAnalysis
@{
    Layout = "~/Views/Shared/Master.cshtml";
    var serverInfo = Module.GetInstance(Model.ConnectionInfo);
}
@functions
{
    string TopKeys(KeyStats ks)
    {
        foreach (var tk in ks.TopKeys)
        {
            @:@tk.Value (@tk.Key.ToComma() bytes)
        }
        return null;
    }
    string Percent(long bytes)
    {
        if (bytes != 0)
        {
            var percent = (bytes / (decimal)Model.TotalByteSize) * 100;
            <span class="text-muted">(@percent.ToString("0.00")%)</span>
        }
        return null;
    }
}
<h5 class="page-header">
    @Model.IconSpan() @serverInfo.HostAndPort (@Model.ConnectionInfo.Name) - Database @Model.Database.ToString()
    <a class="pull-right" href="@Url.Action(nameof(RedisController.ClearAnalysis), new { node = serverInfo.HostAndPort, db = Model.Database })">Re-run analysis</a>
</h5>
<div class="row small">
    <div class="value-block col-md-2 col-sm-4 col-xs-6">
        @Model.Count.ToComma()
        <label>Keys</label>
    </div>
    <div class="value-block col-md-2 col-sm-4 col-xs-6">
        @Model.TotalByteSize.ToHumanReadableSize()
        <label>Total Size</label>
    </div>
    <div class="value-block col-md-2 col-sm-4 col-xs-6">
        @Model.KeyByteSize.ToHumanReadableSize()
        <label>Key Size</label>
    </div>
    <div class="value-block col-md-2 col-sm-4 col-xs-6">
        @Model.ValueByteSize.ToHumanReadableSize()
        <label>Value Size</label>
    </div>
    <div class="value-block col-md-2 col-sm-4 col-xs-6">
        @Model.CreationDate.ToRelativeTimeSpan()
        <label>Ran</label>
    </div>
    <div class="value-block col-md-2 col-sm-4 col-xs-6">
        @Model.TotalTime.ToReadableString()
        <label>Runtime</label>
    </div>
</div>
<h5 class="page-header">
    Memory Analysis
</h5>
<table class="table table-striped table-hover text-nowrap table-super-condensed table-responsive">
    <thead>
        <tr>
            <th>Key Pattern</th>
            <th>Count</th>
            <th>Bytes (Key)</th>
            <th>Bytes (Value)</th>
            <th>Bytes (Total)</th>
        </tr>
    </thead>
    <tbody>
        @if (Model.ErrorMessage.HasValue())
        {
            <tr>
                <td colspan="5">
                    <div class="alert alert-warning">
                        <h5>Error</h5>
                        <p>@Model.ErrorMessage</p>
                    </div>
                </td>
            </tr>
        }
        @foreach (var s in Model.KeyStats.OrderByDescending(s => s.Value.TotalByteSize))
        {
            var ks = s.Value;
            if (s.Value.Count > 0)
            {
                <tr>
                    <td title="Regex: @s.Key.Regex.ToString()
                        @TopKeys(s.Value) ">
                        @s.Key.Name
                    </td>
                    <td>@ks.Count.ToComma()</td>
                    <td>@ks.KeyByteSize.ToComma()</td>
                    <td>@ks.ValueByteSize.ToComma()</td>
                    <td>@ks.TotalByteSize.ToComma() @Percent(ks.TotalByteSize)</td>
                </tr>
            }
        }
    </tbody>
    <tfoot>
        <tr class="total-row">
            <td><b>Total</b></td>
            <td>@Model.Count.ToComma()</td>
            <td>@Model.KeyByteSize.ToHumanReadableSize()</td>
            <td>@Model.ValueByteSize.ToHumanReadableSize()</td>
            <td>@Model.TotalByteSize.ToHumanReadableSize()</td>
        </tr>
    </tfoot>
</table>
@if (Model.TopKeys.Any())
{
    <h5 class="page-header">
        Top Keys (by Size)
    </h5>
    <table class="table table-striped table-hover text-nowrap table-super-condensed table-responsive">
        <thead>
            <tr>
                <th>Category</th>
                <th>Key</th>
                <th>Bytes (Key)</th>
                <th>Bytes (Value)</th>
                <th>Bytes (Total)</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var tk in Model.TopKeys)
            {
                <tr>
                    <td>@tk.Matcher.Name</td>
                    <td>@tk.Name</td>
                    <td>@tk.KeyBytes.ToComma()</td>
                    <td>@tk.ValueBytes.ToComma()</td>
                    <td>@tk.TotalBytes.ToComma() @Percent(tk.TotalBytes)</td>
                </tr>
            }
        </tbody>
    </table>
}